package cn.fr4nk.crawler.task;


public abstract class AbstractHandler<T> implements Handler<T> {

	protected AbstractHandler<T> nextHandler;
	
	protected abstract void doHandle(T result) throws Exception;
	
	/**
	 * 责任链模式：自己处理了，如果配置了下一个责任人，则也需要处理
	 */
	@Override
	public void handle(T result) throws Exception {
		if (result == null) {
			throw new IllegalArgumentException("TaskResult invalid: null");
		}
		doHandle(result);
		if (null != nextHandler) {
			nextHandler.handle(result);
		}
	}

	public AbstractHandler<T> getNextHandler() {
		return nextHandler;
	}

	public void setNextHandler(AbstractHandler<T> nextHandler) {
		this.nextHandler = nextHandler;
	}
	
}
